import router from '@/router';
import { VxeUI } from 'vxe-pc-ui';
import { smartTranslate as $t, transPlaceholder as $p } from '@/lang';
import { hasAuth } from '@/plugins/permission';
import { useComponentState } from '@/composables/useComponentState';

import { useResetPwdModal } from '@/composables/useResetPwdModal.js';


export const useButtons = (options = {}) => {
  const { selectedCount, currentRow } = options;
  const { isChildrenMode } = useComponentState();
  
  return [
    {
      name: $t('common.action.back', '返回'),
      code: 'return_code',
      status: 'info',
      icon: 'Back',
      visible: computed(() => {
        return isChildrenMode.value;
      }),
    },
    {
      name: $t('common.button.add', '新增'),
      code: 'add_code',
      status: 'primary',
      icon: 'Plus',
      visible: hasAuth('admin:user:add'),
      disabled: computed(() => {
        return false;
      }),
    },
    {
      name: $t('common.button.update', '修改'),
      code: 'update_code',
      status: 'warning',
      icon: 'Edit',
      visible: hasAuth('admin:user:update'),
      disabled: computed(() => {
        return selectedCount.value != 1;
      }),
    },
    {
      name: $t('common.action.delete', '删除'),
      code: 'delete_code',
      status: 'danger',
      icon: 'Delete',
      visible: hasAuth('admin:user:delete'),
      disabled: computed(() => {
        return selectedCount?.value === 0;
      }),
    },
    {
      name: $t('common.button.detail', '详情'),
      code: 'detail_code',
      status: 'info',
      icon: 'View',
      visible: hasAuth('admin:user:detail'),
      disabled: computed(() => {
        return selectedCount.value != 1;
      }),
    },
    {
      name: $t('common.action.reset', '重置密码'),
      code: 'reset_code',
      status: 'danger',
      icon: 'Lock',
      visible: hasAuth('admin:user:reset'),
      disabled: computed(() => {
        return selectedCount.value != 1;
      }),
    },


    // 结束
  ];
};

export const useActions = (options = {}) => {
  const { API, gridRef, addRef,updateRef,detailRef,resetRef, } = options;
  


  // 统一确认对话框消息
  const confirmMessage = (action, ids) => {
    const confirmMessage = $t('common.message.confirm.general', '您确定要执行{action}', { params: { action },});
    return `${confirmMessage} ：${ids.join(', ')}`;
  };

  const actions = {
    addHandle() {
      addRef.value?.show();
    },
    updateHandle({ rows }) {
      updateRef.value.show(rows[0].id);
    },
    deleteHandle({ rows }) {
      const ids = rows.map((item) => item.id);
      const content = confirmMessage($t('common.action.delete', '删除'), ids);
      VxeUI.modal.confirm(content).then((result) => {
        if (result === 'confirm') {
          API.delete({ id: ids })
            .then(() => {
              gridRef.value.commitProxy('query');
              ElMessage.success($t('common.message.success', '操作成功'));
            })
            .catch((error) => {
              ElMessage.success($t('common.message.error', '操作失败'));
            });
        }
      });
    },
    detailHandle({ rows }) {
      detailRef.value.show(rows[0].id);
    },
    resetHandle({ rows }) {
      const row = rows[0];
      const modal = useResetPwdModal(app, {
        api: API.reset,
        $t,
        onReload: () => {
          gridRef.value.commitProxy('query');
        },
      });
      modal.showModal(row.id);
    },


    // 结束
  };

  // 行操作按钮的通用调用方法
  const dispatchAction = (code, row) => {
    switch (code) {
      case 'update_handle':
        actions.updateHandle({ rows: [row] });
        break;
      case 'detail_handle':
        actions.detailHandle({ rows: [row] });
        break;
      default:
        console.warn(`Unknown action code: ${code}`);
    }
  };

  return { ...actions, dispatchAction };
};